<template>
  <a-modal
    title="添加到团战"
    :visible="visible"
    :footer="null"
    :width="800"
    @cancel="hide"
  >
    <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-form-model-item label="目标ID">
        <a-input disabled v-model="form.missionId" />
      </a-form-model-item>
      <a-form-model-item label="目标内容">
        <a-textarea disabled v-model="record.content" />
      </a-form-model-item>
      <a-form-model-item label="团战">
        <a-select v-model="form.instanceId" :loading="loading" :filterOption="instanceFilterOption" :showSearch="true" >
          <a-select-option v-for="item in instanceOptions" :key="item.id" :value="item.id" :label="item.title">{{ item.title }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 18, offset: 3 }" style="text-align: center">
        <a-button type="primary" @click="onSubmit" :loading="loading">
          添加
        </a-button>
        <a-button style="margin-left: 10px;" @click="hide" :loading="loading">
          取消
        </a-button>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
// tool
import moment from 'moment'

// API
import { fetchMyInstances, addMission } from '@/api/instance'

export default {
  name: 'AddInstance',
  data () {
    return {
      visible: false, // 是否显示模态框
      loading: false, // 是否加载
      labelCol: { offset: 3, span: 4 },
      wrapperCol: { span: 14 },
      form: {},
      record: {},
      instanceOptions: []
    }
  },
  methods: {
    show (record) {
      this.form.missionId = record.id
      this.record = record
      this.visible = true
      this.init()
    },
    hide () {
      this.visible = false
    },
    reset () {
      this.form = {}
      this.record = {}
    },
    async init () {
      try {
        this.loading = true
        const content = await fetchMyInstances({
          pageNumber: 1,
          pageSize: 1000,
          state: 'RECRUIT'
        })
        console.log(content)
        this.instanceOptions = content
      } catch (error) {

      } finally {
        this.loading = false
      }
    },
    async onSubmit () {
      try {
        this.loading = true
        console.log('this.form!', this.form)
        const params = { ...this.form }
        params.dueDate = moment(this.form.dueDate).format('YYYYMMDD')
        await addMission(params)
        this.hide()
        this.reset()
      } catch (error) {

      } finally {
        this.loading = false
      }
    },
    instanceFilterOption (inputValue, option) {
      return option.componentOptions.propsData.label.indexOf(inputValue) > -1
    }
  }
}
</script>
